package io.renren.common.utils;

import io.renren.common.exception.RRException;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author daijiangping
 */
public class StringUtil {
    /**
     * 生成uuid+时间错
     *
     * @return
     */
    public static String uuid() {
        return UUID.randomUUID().toString().replace("-", "") + System.nanoTime();
    }

    public static String langKey() {
        return "LANG_" + UUID.randomUUID().toString().replace("-", "") + System.nanoTime();
    }

    /**
     * 订单号生成方式
     *
     * @return
     */

    public static String orderNo() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date();
        String str = sdf.format(date);
        String OrderNo = str + (System.currentTimeMillis() + "").substring(1) +
                (System.nanoTime() + "").substring(7, 10);
        return OrderNo;
    }

    /**
     * 如果是空返回空
     *
     * @return
     */
    public static String isNull(Object obj) {
        if (obj == null || "".equals(obj.toString())) {
            return "";
        } else {
            return obj.toString();
        }

    }

    /**
     * 如果是空返回空
     *
     * @return
     */
    public static boolean isNullBoolean(Object obj) {
        if (obj == null || "".equals(obj.toString().trim())) {
            return true;
        } else {
            return false;
        }

    }

    /**
     * 判断某个字符串在某个数组中没有
     *
     * @return
     */
    public static boolean useList(String[] arr, String targetValue) {
        return Arrays.asList(arr).contains(targetValue);
    }

    /**
     * 将逗号分割的的某个值贴换成某个值
     *
     * @param wait
     * @param target
     * @return
     */
    public static String replaseArr(String arrStr, String wait, String target) {

        if (isNullBoolean(arrStr)) {
            return "";
        }
        try {
            Set<String> numList = new HashSet<>();

            for (String id : arrStr.split(",")) {
                if (!wait.equals(id)) {
                    numList.add(id);
                } else {
                    numList.add(target);
                }
            }
            return org.apache.commons.lang.StringUtils.join(numList.toArray(), ",");
        } catch (Exception e) {
            e.printStackTrace();
            return arrStr;
        }
    }

    public static void checkParams(String... params) {
        for (String s : params) {
            if (StringUtil.isNullBoolean(s)) {
                throw new RRException("params is null");
            }
        }
    }
}
